package class4;

/**
 * https://leetcode.cn/problems/er-jin-zhi-zhong-1de-ge-shu-lcof/
 * 位1的个数
 */
public class Code05_hammingWeight {
    public static void main(String[] args) {
        int n = -3;
        int total = hammingWeight(n);
        System.out.println(total);
        int i = hammingWeight2(n);
        System.out.println("i = " + i);
    }

    public static int hammingWeight(int n) {
        int total = 0;
        while (n != 0) {
            total += n & 1;
            n >>>= 1;
        }
        return total;
    }

    public static int hammingWeight2(int n) {
        int total = 0;
        while (n != 0) {
            n &= (n - 1);
            total++;
        }
        return total;
    }
}
